package com.hgkj.cloud.base.util;

import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Author: yangcg
 */
@Slf4j
public class AssertUtils {
    // 手机号
    private static String mobileNumRegex = "^(0|[1-9][0-9]*)$";
    // 数字
    private static String numRegex = "^[0-9]*$";
    public static final String FORMAT_DATE_TIME = "yyyy-MM-dd HH:mm:ss";
    public static final String FORMAT_DATE = "yyyy-MM-dd";

    /**
     * 获得某天的起始时间
     *
     * @return
     */
    public static Calendar getStartDate(Calendar today) {
        today.set(Calendar.HOUR_OF_DAY, 0);
        today.set(Calendar.MINUTE, 0);
        today.set(Calendar.SECOND, 0);
        return today;
    }

    /**
     * 获取某天截止时间
     *
     * @return
     */
    public static Calendar getEndDate(Calendar endToday) {
        endToday.set(Calendar.HOUR_OF_DAY, 23);
        endToday.set(Calendar.MINUTE, 59);
        endToday.set(Calendar.SECOND, 59);
        return endToday;
    }

    /**
     * 获得某天的起始时间
     *
     * @return
     */
    public static Calendar getStartDate(Date date) {
        Calendar start = Calendar.getInstance();
        start.setTime(date);
        start.set(Calendar.HOUR_OF_DAY, 0);
        start.set(Calendar.MINUTE, 0);
        start.set(Calendar.SECOND, 0);
        return start;
    }

    /**
     * 获取某天截止时间
     *
     * @return
     */
    public static Calendar getEndDate(Date date) {
        Calendar end = Calendar.getInstance();
        end.setTime(date);
        end.set(Calendar.HOUR_OF_DAY, 23);
        end.set(Calendar.MINUTE, 59);
        end.set(Calendar.SECOND, 59);
        return end;
    }

    public static Date getMonthStartDay(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        return calendar.getTime();
    }

    public static Date getMonthEndDay(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        return calendar.getTime();
    }

    /***
     * 日期加n天
     * @param date
     * @param num
     */
    public static Date dateAdd(Date date, int num) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.get(Calendar.DAY_OF_MONTH) + num);//让日期加1
        return calendar.getTime();
    }

    /***
     * 日期减n天
     * @param date
     * @param num
     */
    public static Date dateSub(Date date, int num) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.get(Calendar.DAY_OF_MONTH) - num);//让日期减1
        return calendar.getTime();
    }
    /***
     * 去除list中null值
     * @param list
     * @return
     */
    public static List removeListNull(List list) {
        List objectList = new ArrayList();
        objectList.add(null);
        list.removeAll(objectList);
        return list;
    }

    /***
     * 日期加减几个月
     * @param date
     * @param num
     */
    public static Date dateMonthAddSub(Date date, int num) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, num);
        return calendar.getTime();
    }

    public static String strFormat(String txt) {
        String value = txt;
        try {
            if (txt.contains(".")) {
                String[] split = txt.split("\\.");
                if (split.length == 2) {
                    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
                    BigDecimal bd = new BigDecimal(txt);
                    int days = bd.intValue();
                    int mills = (int) Math.round(bd.subtract(new BigDecimal(days)).doubleValue() * 24 * 3600);
                    Calendar c = Calendar.getInstance();
                    c.set(1900, 0, 1);
                    c.add(Calendar.DATE, days - 2);
                    int hour = mills / 3600;
                    int minute = (mills - hour * 3600) / 60;
                    int second = mills - hour * 3600 - minute * 60;
                    c.set(Calendar.HOUR_OF_DAY, hour);
                    c.set(Calendar.MINUTE, minute);
                    c.set(Calendar.SECOND, second);
                    //转化之后要获取的值
                    Date d = c.getTime();//Date类型
                    txt = dateFormat.format(d);
                }
                if (txt.contains(":")) {
                    if (txt.contains(" ")) {
                        String[] s = txt.split(" ");
                        txt = s[0];
                    }
                }
                txt = txt.replaceAll("\\.", "-");
            }
            if (txt.contains(".")) {
                txt = txt.replaceAll("\\.", "-");
            }
            if (txt.contains("/")) {
                txt = txt.replaceAll("/", "-");
            }
            if (txt.contains("年") || txt.contains("月") || txt.contains("日")) {
                txt = txt.replaceAll("年", "-");
                txt = txt.replaceAll("月", "-");
                txt = txt.replaceAll("日", "");
            }
            return txt;
        } catch (Exception e) {
            return value;
        }
    }

    /***
     * 根据属性名获取属性值
     * @param fieldName
     * @param obj
     * @return
     */
    public static Object getFieldValueByName(String fieldName, Object obj) {
        try {
            String firstLetter = fieldName.substring(0, 1).toUpperCase();
            String getter = "get" + firstLetter + fieldName.substring(1);
            Method method = obj.getClass().getMethod(getter, new Class[]{});
            Object value = method.invoke(obj, new Object[]{});
            return value;
        } catch (Exception e) {
            return null;
        }
    }


}



